import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { title: '登录', requiresAuth: false }
    },
    {
      path: '/',
      component: () => import('@/layouts/MainLayout.vue'),
      redirect: '/dashboard',
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/Dashboard.vue'),
          meta: { title: '首页', icon: 'HomeFilled' }
        },
        // 人员管理模块
        {
          path: 'personnel/import',
          name: 'PersonnelImport',
          component: () => import('@/views/personnel/Import.vue'),
          meta: { title: '人员管理', icon: 'User', subtitle: '数据导入' }
        },
        {
          path: 'personnel/process',
          name: 'PersonnelProcess',
          component: () => import('@/views/personnel/Process.vue'),
          meta: { title: '人员管理', icon: 'User', subtitle: '数据处理' }
        },
        {
          path: 'personnel/audit',
          name: 'PersonnelAudit',
          component: () => import('@/views/personnel/Audit.vue'),
          meta: { title: '人员管理', icon: 'User', subtitle: '数据审核' }
        },
        {
          path: 'personnel/list',
          name: 'PersonnelList',
          component: () => import('@/views/personnel/List.vue'),
          meta: { title: '人员管理', icon: 'User', subtitle: '人员列表' }
        },
        // 武器装备模块
        {
          path: 'weapon/import',
          name: 'WeaponImport',
          component: () => import('@/views/weapon/Import.vue'),
          meta: { title: '武器装备', icon: 'Warning', subtitle: '数据录入' }
        },
        {
          path: 'weapon/process',
          name: 'WeaponProcess',
          component: () => import('@/views/weapon/Process.vue'),
          meta: { title: '武器装备', icon: 'Warning', subtitle: '数据加工' }
        },
        {
          path: 'weapon/audit',
          name: 'WeaponAudit',
          component: () => import('@/views/weapon/Audit.vue'),
          meta: { title: '武器装备', icon: 'Warning', subtitle: '数据审核' }
        },
        {
          path: 'weapon/list',
          name: 'WeaponList',
          component: () => import('@/views/weapon/List.vue'),
          meta: { title: '武器装备', icon: 'Warning', subtitle: '装备列表' }
        },
        // 系统管理
        {
          path: 'system/users',
          name: 'SystemUsers',
          component: () => import('@/views/system/Users.vue'),
          meta: { title: '系统管理', icon: 'Setting', subtitle: '用户管理' }
        },
        {
          path: 'system/health',
          name: 'SystemHealth',
          component: () => import('@/views/system/Health.vue'),
          meta: { title: '系统管理', icon: 'Setting', subtitle: '系统状态' }
        }
      ]
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 军事数据加工平台`
  }

  // 检查是否需要认证
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth !== false)
  
  if (requiresAuth) {
    // 需要认证的页面
    if (!userStore.isLoggedIn) {
      // 未登录，跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 记录目标路径，登录后可以跳转回来
      })
    } else {
      // 已登录，检查是否有用户信息
      if (!userStore.userInfo) {
        // 尝试获取用户信息
        const success = await userStore.getUserInfo()
        if (success) {
          next()
        } else {
          // 获取失败，token可能失效，跳转到登录页
          next('/login')
        }
      } else {
        next()
      }
    }
  } else {
    // 不需要认证的页面
    if (to.path === '/login' && userStore.isLoggedIn) {
      // 已登录用户访问登录页，跳转到首页
      next('/dashboard')
    } else {
      next()
    }
  }
})

export default router

